package com.jdbcsimple.datasource;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

/**
 * abstract base class for JdbcSimple's {@link javax.sql.DataSource} implementations.
 * @author Leonard Lan
 * @since 07.04.2013
 */
public abstract class AbstractDataSource implements DataSource {

	@Override
	public PrintWriter getLogWriter() throws SQLException {
		throw new UnsupportedOperationException("getLogWriter");
	}

	@Override
	public int getLoginTimeout() throws SQLException {
		return 0;
	}

	@Override
	public void setLogWriter(PrintWriter out) throws SQLException {
		throw new UnsupportedOperationException("setLogWriter");
	}

	@Override
	public void setLoginTimeout(int seconds) throws SQLException {
		throw new UnsupportedOperationException("setLoginTimeout");
	}

	@Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException {		
		return DataSource.class.equals(iface);
	}

	@Override
	public <T> T unwrap(Class<T> iface) throws SQLException {
		if(iface == null){
			throw new IllegalArgumentException("Interface argument must not be null");
		}
		if(!DataSource.class.equals(iface)){
			throw new SQLException("DataSource of type [" + getClass().getName() +
					"] can only be unwrapped as [javax.sql.DataSource], not as [" + iface.getName());
		}
		return (T)this;
	}

}
